<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    姓: <input type="text" v-model="surname"> +
    名: <input type="text" v-model="name"> = 
    <span>{{ fullName }}</span><br><br>

    <button @click="changeName">改名卡</button>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        surname: '',
        name: ''
      },
      methods: {
        changeName() {
          this.fullName = '吕小布'
        }
      },
      computed: {
        // comouted简写不能修改,只能读取访问
        // fullName() {
        //   return this.surname + this.name
        // }

        // 完整写法 -> 获取 + 设置
        fullName: {
          // (1)当fullName计算属性,被获取求值时,执行get(有缓存,优先读缓存)
          // 会将返回值作为求值的结果
          get() {
            return this.surname + this.name
          },
          // (2)当fullName计算属性,被修改赋值时,执行set
          // 修改的值,传递给set方法的形参
          set(value) {
            this.surname = value.slice(0, 1)
            this.name = value.slice(1)
          }
        }
      }
    })
  </script>
</body>
</html>